package com.dwx.guli.service.trade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dwx.guli.service.base.dto.CourseDto;
import com.dwx.guli.service.base.dto.MemberDto;
import com.dwx.guli.service.base.exception.GuliException;
import com.dwx.guli.service.base.result.R;
import com.dwx.guli.service.base.result.ResultCodeEnum;
import com.dwx.guli.service.trade.entity.Order;
import com.dwx.guli.service.trade.entity.PayLog;
import com.dwx.guli.service.trade.feign.EduCourseService;
import com.dwx.guli.service.trade.feign.UcenterMemberService;
import com.dwx.guli.service.trade.mapper.OrderMapper;
import com.dwx.guli.service.trade.mapper.PayLogMapper;
import com.dwx.guli.service.trade.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dwx.guli.service.trade.service.PayLogService;
import com.dwx.guli.service.trade.util.OrderNoUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.wxpay.sdk.WXPayUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author dwx
 * @since 2021-07-18
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    UcenterMemberService ucenterMemberService;
    @Autowired
    PayLogService payLogService;
    @Override
    public String saveOrder(String courseId, String memberId) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.eq("member_id", memberId);
        Order order = baseMapper.selectOne(queryWrapper);
        if(order!=null){
            return order.getId();
        }
        R rCourse = eduCourseService.getCourseDtoById(courseId);
        ObjectMapper mapper=new ObjectMapper();
        CourseDto courseDto = mapper.convertValue(rCourse.getData().get("courseDto"), CourseDto.class);
        if(courseDto == null){
            throw new GuliException(ResultCodeEnum.REMOTE_CALL_ERROR);
        }
        R rMember = ucenterMemberService.getMemberDtoById(memberId);
        MemberDto memberDto = mapper.convertValue(rMember.getData().get("memberDto"), MemberDto.class);
        if(memberDto == null){
            throw new GuliException(ResultCodeEnum.REMOTE_CALL_ERROR);
        }
        order=new Order();
        //创建订单
        order.setOrderNo(OrderNoUtils.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        Long totalFee = courseDto.getPrice().multiply(new BigDecimal(100)).longValue();
        order.setTotalFee(totalFee);//分
        order.setMemberId(memberId);
        order.setMobile(memberDto.getMobile());
        order.setNickname(memberDto.getNickname());
        order.setStatus(0);//未支付
        order.setPayType(1);//微信支付
        baseMapper.insert(order);
        return order.getId();
    }

    @Override
    public Boolean isBuyByCourseId(String courseId, String memberId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("member_id", memberId)
                .eq("course_id", courseId)
                .eq("status", 1);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count.intValue() > 0;
    }

    @Override
    public Order getByOrderId(String id, String memberId) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("member_id", memberId);
        Order order = baseMapper.selectOne(queryWrapper);
        return order;
    }

    @Override
    public boolean removeById(String orderId, String memberId) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",orderId);
        queryWrapper.eq("member_id",memberId);
        return this.remove(queryWrapper);
    }

    @Override
    public void updateOrderStatus(Map<String, String> notifyMap) {
        String tradeNo = notifyMap.get("out_trade_no");
        baseMapper.updateStatusByOrderNo(tradeNo);
        payLogService.createPayLog(notifyMap);
        Order order = this.getOrderByOrderNo(tradeNo);
        eduCourseService.updateBuyCountById(order.getCourseId());
    }

    @Override
    public boolean queryPayStatus(String orderNo) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        Order order = baseMapper.selectOne(queryWrapper);
        return order.getStatus()==1;
    }

    @Override
    public Order getOrderByOrderNo(String orderNo) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Order> selectByMemberId(String memberId) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.eq("member_id",memberId);
        List<Order> orders = baseMapper.selectList(queryWrapper);
        return orders;
    }
}
